/*
 * $Header: /home/cvsroot/ctvit/messgeCenter/src/com/ctvit/framework/service/BaseService.java,v 1.4 2016/04/14 01:26:04 yushihao Exp $
 */
package com.ctvit.framework.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

import com.ctvit.framework.dao.GenericDao;
import com.ctvit.struts.common.CommonFunc;

public abstract class BaseService implements Serializable {
	protected final Log log = LogFactory.getLog(getClass());
	private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPort>();
	static {
		redisHostAndPortList.add(new HostAndPort(CommonFunc.redisHost,CommonFunc.redisPort));
	}
	private static HostAndPort hnp = redisHostAndPortList.get(0);
	protected final boolean isDebugEnabled = log.isDebugEnabled();

	protected GenericDao genericDao;

	public final void setGenericDAO(GenericDao genericDao) {
		this.genericDao = genericDao;
	}

	public final Authentication getLoginUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication != null && authentication.isAuthenticated()) {
			return authentication;
		}
		return null;
	}

	public final String getCurrentUsername() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication != null && authentication.isAuthenticated()) {
			return authentication.getName();
		}
		return null;
	}
	public String createSequence(String seq){
		String sNo="";
		String sqlString="select getnextval('"+seq+"') nextval";
		List lstList=genericDao.executeSql(sqlString);
		if (lstList!=null&&lstList.size()>0){
			HashMap map = (HashMap)lstList.get(0);
			sNo=String.valueOf(map.get("nextval"));
		}
		return sNo;
	}
	public Jedis getJedisConn(){
		JedisPool pool=null;
		Jedis jedis =null;
		try {
			pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
		    jedis = pool.getResource();
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return jedis;
	}
	public List execSql(String sql) {
		return genericDao.executeSql(sql);
	}
}
